﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ProgNetComponents.MDI
{
    public class FormCreator
    {
        public event FormCreateEventHandler FormCreated;
        public event FormClosedEventHandler FormClosed;

        public Form CreateForm(Type childType, params object[] args)
        {
            Form f = null;
            f = Activator.CreateInstance(childType, args) as Form;
            if (f != null)
            {
                f.FormClosed += new FormClosedEventHandler(OnFormClosed);
                if (FormCreated != null)
                    FormCreated(this, new FormCreateEventArgs(f));
            }
            return f;
        }

        private void OnFormClosed(object sender, FormClosedEventArgs e)
        {
            if (FormClosed != null)
                FormClosed(sender, e);
            Form f = sender as Form;
            f.FormClosed -= new FormClosedEventHandler(OnFormClosed);
        }

        public T1 CreateForm<T1>(params object[] parameters) where T1 : Form
        {
            return (T1)CreateForm(typeof(T1), parameters);
        }
    }
}
